#define CACHE_ENABLED

using System;
using System.Reflection;
using System.Collections;


namespace Handrise.Mobile.BasicModal.Serialization
{
    /// <summary>
    /// Class Inspector is the class responsible to extract fields from 
    /// a given type.
    /// The define symbol CACHE_ENABLED is used to activate field cache table.
    /// For each type serialized a copy of its field list is cached, therefore
    /// eliminating delay caused by reflection.
    /// </summary>
    public class ClassInspector
    {
#if CACHE_ENABLED
		private static Hashtable ReflectionCache = new Hashtable();
#endif

        public static void Clear()
        {
#if CACHE_ENABLED
			ReflectionCache.Clear();
#endif
        }

        public static FieldInfo[] InspectClass(Type type)
        {
#if CACHE_ENABLED
			if (ReflectionCache.Contains(type))
				return (FieldInfo[])ReflectionCache[type];
#endif

            FieldInfo[] array = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            ArrayList list = new ArrayList(array);

            //FieldInfo[] list = new FieldInfo[array.Length];
            //int index = 0;

            for (int i = 0; i < array.Length; i++)
            {
                //I'm going to consider only fields not marked with the 
                //NotSerialized attribute
                // !array[i].FieldType.IsPointer this was the old CFormatter, is still necessary?
                if (array[i].GetCustomAttributes(typeof
                    (Attributes.NotSerializedAttribute), false).Length != 0)
                    list.Remove(array[i]);
            }

            return (FieldInfo[])list.ToArray(typeof(FieldInfo));
        }
    }
}
